#tag Class
Protected Class Player
	#tag Method, Flags = &h0
		Sub Constructor(name as string, playerColor as color, type as integer, aiType as integer, theGame as concreteGame)
		  dim i as integer
		  dim gp as Gamepiece
		  
		  
		  self.name = name
		  self.playerColor = playerColor
		  self.type = type               '1 for human, 2 for AI
		  self.aiType = aiType           '-1 no AI (if human player)
		  self.game = theGame
		  
		  addTileActionState = new AddTileActionState(self)
		  occupyActionState = new OccupyActionState(self)
		  calculateScoreActionState = new CalculateScoreActionState(self)
		  offTurnActionState = new OffTurnActionState(self)
		  actionState = offTurnActionState
		  
		  for i = 0 to 6
		    gp = new Gamepiece(self)
		    gp.setUid name + str(i + 1)
		    gamepieces.Append gp
		  next i
		  
		  if type = 2 then
		    if aiType = 1 then
		      ai = new RandomGuy(self)
		    elseif aiType = 2 then
		      ai = new DecissionTree(self)
		    end if
		  end if
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetName() As String
		  return name
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetColor() As color
		  return PlayerColor
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetScore() As integer
		  return score
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub SetScore(s as integer)
		  score = score + s
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetType() As integer
		  return type
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetAiType() As integer
		  return aiType
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetActionState() As PlayerActionStates
		  return actionState
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetNextTile() As Tile
		  return actionState.GetNextTile
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetGame() As ConcreteGame
		  return game
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function AddTileAtPosition(newTile as Tile, position as Coordinates) As boolean
		  if actionState.AddTileAtPosition(newTile, position) = true then
		    NextPhase
		    return true
		  else
		    return false
		  end if
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub RotateClockwise()
		  actionState.RotateClockwise
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub RotateCounterclockwise()
		  actionState.RotateCounterclockwise
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub Activate()
		  if actionState.toString = "OffTurnActionState" then
		    actionState = addTileActionState
		  end if
		  
		  if type = 2 then
		    ai.calculate
		  end if
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub NextPhase()
		  select case actionState.ToString
		  case "AddTileActionState"
		    actionState = occupyActionState
		  case "OccupyActionState"
		    actionState = calculateScoreActionState
		  case "CalculateScoreActionState"
		    actionState = offTurnActionState
		  else
		    actionState = offTurnActionState
		  end select
		  
		  
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub Occupy(s as string)
		  //occupies a slot at position s
		  //s is a string and describes one of the nine possible
		  //slot-positions (ie. nw, n, ne, w, c, e, sw, s, se)
		  actionState.occupy s
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetGamepiece() As gamepiece
		  dim gp as Gamepiece
		  
		  gp = gamePieces.Pop
		  
		  return gp
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function HasGamepieces() As boolean
		  if ubound(gamePieces) <> -1 then
		    return true
		  else
		    return false
		  end if
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub ReturnGamepiece(gp as gamepiece)
		  //return the gamepiece in the stack of gamepieces, remove it from the henchmen stack
		  dim i as integer
		  
		  for i = 0 to ubound(henchmen)
		    if henchmen(i).GetUid = gp.GetUid then
		      henchmen.Remove i
		      gamePieces.Append gp
		      exit for
		    end if
		  next i
		  
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub EndRound()
		  actionState.EndRound
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub SwitchTo(i as integer)
		  #if DebugBuild
		    //only for testing purposes
		    //1 = AddTileActionState
		    //2 = occupyActionState
		    //3 = calculateScoreActionState
		    //4 = offTurnActionState
		    select case i
		    case 1
		      actionState = addTileActionState
		    case 2
		      actionState = occupyActionState
		    case 3
		      actionState = calculateScoreActionState
		    case 4
		      actionState = offTurnActionState
		    end select
		  #endif
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub AddHenchman(gp as gamepiece)
		  henchmen.append gp
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub EndGame()
		  Game.EndGame
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetNumberOfGamepieces() As integer
		  return UBound(gamePieces)+1
		End Function
	#tag EndMethod


	#tag Property, Flags = &h21
		Private name As String
	#tag EndProperty

	#tag Property, Flags = &h21
		Private playerColor As color
	#tag EndProperty

	#tag Property, Flags = &h21
		Private score As Integer = 0
	#tag EndProperty

	#tag Property, Flags = &h21
		Private type As Integer
	#tag EndProperty

	#tag Property, Flags = &h21
		Private aiType As Integer
	#tag EndProperty

	#tag Property, Flags = &h21
		Private game As ConcreteGame
	#tag EndProperty

	#tag Property, Flags = &h21
		Private actionState As PlayerActionStates
	#tag EndProperty

	#tag Property, Flags = &h21
		Private addTileActionState As PlayerActionStates
	#tag EndProperty

	#tag Property, Flags = &h21
		Private occupyActionState As PlayerActionStates
	#tag EndProperty

	#tag Property, Flags = &h21
		Private calculateScoreActionState As PlayerActionStates
	#tag EndProperty

	#tag Property, Flags = &h21
		Private offTurnActionState As PlayerActionStates
	#tag EndProperty

	#tag Property, Flags = &h21
		Private gamePieces() As GamePiece
	#tag EndProperty

	#tag Property, Flags = &h21
		Private henchmen() As Gamepiece
	#tag EndProperty

	#tag Property, Flags = &h21
		Private ai As ArtificialPlayer
	#tag EndProperty


	#tag ViewBehavior
		#tag ViewProperty
			Name="Name"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Index"
			Visible=true
			Group="ID"
			InitialValue="-2147483648"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Super"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Left"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Top"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
	#tag EndViewBehavior
End Class
#tag EndClass
